package cn.tedu.tmall.admin.content.dao.repository.impl;

import cn.tedu.tmall.admin.content.dao.mapper.CategoryMapper;
import cn.tedu.tmall.admin.content.dao.repository.ICategoryRepository;
import cn.tedu.tmall.admin.content.pojo.po.CategoryPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 数据操作层-类别接口实现类
 */
@Slf4j
@Repository
public class CategoryRepository implements ICategoryRepository {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int countCategoryByName(String name) {
        //name赋值给QueryWrapper
        QueryWrapper<CategoryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        //查询是否有符合的记录
        Integer count = categoryMapper.selectCount(queryWrapper);
        log.debug("Repository-查询到符合的记录条数:{}",count);
        return count;
    }

    @Override
    public void save(CategoryPO newCategoryPO) {
        categoryMapper.insert(newCategoryPO);
    }

    @Override
    public CategoryPO getCategoryById(Long categoryId) {
        return categoryMapper.selectById(categoryId);
    }

    @Override
    public Integer getEnableById(Long categoryId) {
        QueryWrapper<CategoryPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable",categoryId);
        return categoryMapper.selectCount(queryWrapper);
    }
}
